package com.housediyadmin.view
{
	/**
	 * 
	 * @author Bo Zhang
	 * 2011/01/07
	 */	
	import com.adobe.cairngorm.process.ProcessEvent;
	import com.housediyadmin.events.bubbleEvents.CheckNewsIDBubbleEvt;
	import com.housediyadmin.events.bubbleEvents.DeleteNewsBubbleEvt;
	import com.housediyadmin.events.bubbleEvents.EditNewsBubbleEvt;
	import com.housediyadmin.events.goodsEvents.GetGoodsByNewsIDEvent;
	import com.housediyadmin.events.goodsEvents.GetGoodsByTypeIDEvent;
	import com.housediyadmin.events.newsEvents.AddNewsEvent;
	import com.housediyadmin.events.newsEvents.DeleteNewsEvent;
	import com.housediyadmin.events.newsEvents.GetNewsEvent;
	import com.housediyadmin.events.treeEvents.DeleteAreaLevelEvent;
	import com.housediyadmin.events.treeEvents.GetGoodsTreeEvent;
	import com.housediyadmin.model.AreaTreeModel;
	import com.housediyadmin.model.GoodsModel;
	import com.housediyadmin.model.GoodsTreeModel;
	import com.housediyadmin.model.HouseModel;
	import com.housediyadmin.model.NewsModel;
	import com.housediyadmin.service.NewsRemoteObjectService;
	import com.housediyadmin.view.components.EditTreeView;
	import com.housediyadmin.view.components.NewsPopUp;
	import com.housediyadmin.view.components.TreeView;
	import com.housediyadmin.vo.Goods;
	import com.housediyadmin.vo.House;
	import com.housediyadmin.vo.News;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.DateField;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.IFlexDisplayObject;
	import mx.events.CalendarLayoutChangeEvent;
	import mx.events.CloseEvent;
	import mx.events.FlexEvent;
	import mx.events.IndexChangedEvent;
	import mx.events.ListEvent;
	import mx.events.Request;
	import mx.formatters.DateFormatter;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.List;
	import spark.events.IndexChangeEvent;

	public class NewsAdminMediator extends Mediator
	{
		[Inject]
		public var view:NewsAdminView;
		
		[Inject]
		public var goodsTreeModel:GoodsTreeModel;
		
		[Inject]
		public var goodsModel:GoodsModel;
		
		[Inject]
		public var newsModel:NewsModel;
		
		override public function onRegister():void
		{						
			BindingUtils.bindProperty(view.newsList, "dataProvider", newsModel, "newsArray");
			dispatch(new GetNewsEvent(GetNewsEvent.GET_ALL_NEWS));		
			// news相关
			view.addNewsBtn.addEventListener(MouseEvent.CLICK, popUpAddNewsView);
			view.deleteSeveralNewsBtn.addEventListener(MouseEvent.CLICK, deleteSeveralNews);
			
			view.addEventListener(EditNewsBubbleEvt.EDIT_A_NEWS, popUpEditNewsView);
			view.addEventListener(DeleteNewsBubbleEvt.DELETE_A_NEWS, deleteEve);
			view.addEventListener(CheckNewsIDBubbleEvt.ADD_TO_NEWSIDS, addIdToNewsIdsHandler);

		}
		
		private function popUpEditNewsView(event:EditNewsBubbleEvt):void
		{
			newsModel.selectedNews = event.news;
			var popUpEditView:NewsPopUp = new NewsPopUp();
			popUpEditView.currentState = "edit";
			PopUpManager.addPopUp(popUpEditView, contextView);
			PopUpManager.centerPopUp(popUpEditView);
			mediatorMap.createMediator(popUpEditView);
			popUpEditView.addEventListener(CloseEvent.CLOSE, close);	
		}
		
		private function popUpAddNewsView(event:MouseEvent):void
		{
			newsModel.selectedNews = new News(); 
			var popUpEditView:NewsPopUp = new NewsPopUp();
			popUpEditView.currentState = "add";
			PopUpManager.addPopUp(popUpEditView, contextView);
			PopUpManager.centerPopUp(popUpEditView);
			mediatorMap.createMediator(popUpEditView);
			popUpEditView.addEventListener(CloseEvent.CLOSE, close);	
		}
		
		public function close(event:CloseEvent):void
		{
			event.target.removeEventListener(CloseEvent.CLOSE, close);
			PopUpManager.removePopUp(event.target as IFlexDisplayObject);
			mediatorMap.removeMediatorByView(event.target as IFlexDisplayObject);		
		}
		
		private function deleteEve(event:DeleteNewsBubbleEvt):void
		{
			Alert.yesLabel = "是";
			Alert.noLabel = "否";
			Alert.cancelLabel = "取消";
			Alert.show("删除相应的新闻吗？", "", 3, view, deleteAlert);
			newsModel.newsIds = new ArrayCollection();
			newsModel.newsIds.addItem(event.newsId);
		}
		
		private function deleteAlert(event:CloseEvent):void
		{
			if(event.detail == Alert.YES)
			{
				var newsArr:Array = new Array;			
				newsArr.push(newsModel.newsIds[0]);
				dispatch(new DeleteNewsEvent(DeleteNewsEvent.DELETE_NEWS, newsArr));			
			}
		}
		
		private function deleteSeveralNews(event:MouseEvent):void
		{	
			if (newsModel.newsIds.length == 0)
			{
				Alert.show("请选择删除的新闻");
			}
			else
			{
				var newsArr:Array = newsModel.selectedNewsArr;
				
				for each(var news:News in newsArr)
				Alert.yesLabel = "是";
				Alert.noLabel = "否";
				Alert.cancelLabel = "取消";
				Alert.show("删除相应的新闻吗？", "", 1|2|8, view, deleteNewsHandler);
			}
		}
		
		private function deleteNewsHandler(event:CloseEvent):void
		{
			if(event.detail == Alert.YES)
			{
				var newsArr:Array = new Array;
				var i:int = 0
				for (i; i < newsModel.newsIds.length; i++)
				{
					newsArr.push(newsModel.newsIds[i]);
				}
				dispatch(new DeleteNewsEvent(DeleteNewsEvent.DELETE_NEWS, newsArr));
			}		
		}
		
		private function selectTreeNode(event:ListEvent):void
		{
			goodsTreeModel.selectedNode = event.target.selectedItem as XML;
			
			var goodsIDs:Array = new Array;	
			var xmlList:XMLList = event.target.selectedItem.@typeID;
			
			if(xmlList.length()!=0)
			{
				for each(var xml:XML in xmlList)
				{
					goodsIDs.push((int)(xml.toXMLString()));
				}
				dispatch(new GetGoodsByTypeIDEvent(GetGoodsByTypeIDEvent.GET_GOODS_BY_TYPEID, goodsIDs));
			}
		}
		
		private function addIdToNewsIdsHandler(event:CheckNewsIDBubbleEvt):void
		{
			if (newsModel.newsIds.contains(event.newsId))
			{
				var index:int = newsModel.newsIds.getItemIndex(event.newsId);
				newsModel.newsIds.removeItemAt(index);
			}
			else  
			{
				newsModel.newsIds.addItem(event.newsId);
			}
		}
	}
}